package com.xyoto.community.recover;

import com.xyoto.community.cache.CacheInstruction;
import com.xyoto.community.config.TopicUserHotTableConfig;
import com.xyoto.community.constant.DeleteStatus;
import com.xyoto.community.constant.LoveStatus;
import com.xyoto.community.constant.TopicUserHotType;
import com.xyoto.community.entity.comment.DynamicComment;
import com.xyoto.community.entity.comment.DynamicCommentReply;
import com.xyoto.community.entity.love.DynamicCommentLove;
import com.xyoto.community.entity.love.DynamicCommentReplyLove;
import com.xyoto.community.entity.love.DynamicLove;
import com.xyoto.community.entity.main.Dynamic;
import com.xyoto.community.mapper.comment.DynamicCommentMapper;
import com.xyoto.community.mapper.comment.DynamicCommentReplyMapper;
import com.xyoto.community.mapper.love.DynamicCommentLoveMapper;
import com.xyoto.community.mapper.love.DynamicCommentReplyLoveMapper;
import com.xyoto.community.mapper.love.DynamicLoveMapper;
import com.xyoto.community.mapper.main.DynamicMapper;
import com.xyoto.community.mapper.main.UserMapper;
import com.xyoto.community.po.UserPO;
import com.xyoto.community.utils.RedisCommonUtils;
//import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

/**
 * Author: 廖振辉
 * Date: 2021/12/6
 * Description: 用户总榜的热度恢复
 * 旧版恢复用户热度的热度数据恢复类
 */
//@SpringBootTest
public class UserHotRecoverOld {

    @Autowired
    private DynamicLoveMapper dynamicLoveMapper;

    @Autowired
    private RedisCommonUtils redisCommonUtils;

    @Autowired
    private TopicUserHotTableConfig topicUserHotTableConfig;

    @Autowired
    private DynamicCommentMapper dynamicCommentMapper;

    @Autowired
    private DynamicCommentReplyMapper dynamicCommentReplyMapper;

    @Autowired
    private DynamicMapper dynamicMapper;

    @Autowired
    private DynamicCommentLoveMapper dynamicCommentLoveMapper;

    @Autowired
    private DynamicCommentReplyLoveMapper dynamicCommentReplyLoveMapper;

    @Autowired
    private UserMapper userMapper;

    /**
     * 恢复用户总榜的用户热度值
     * @author 廖振辉
     * @return {@link }
     */
//    @Test
    public void recover(){
        redisCommonUtils.del(CacheInstruction.USER_RANK);
        recoverDynamicLove();
        recoverComment();
        recoverReply();
        recoverDynamic();
        recoverDynamicBeLoved();
        //
    }

    /**
     * 恢复用户主动点赞产生的热度
     *
     * @return {@link }
     * @author 廖振辉
     */
    void recoverDynamicLove() {
        List<DynamicLove> dynamicLoveList = dynamicLoveMapper.getAll(LoveStatus.LOVE);
        Double score = topicUserHotTableConfig.getScoreByOperateType(TopicUserHotType.LOVE);
        for (DynamicLove dynamicLove : dynamicLoveList) {
            Integer userId = dynamicLove.getUserId();
            redisCommonUtils.incrementScoreForZSet(CacheInstruction.USER_RANK, userId, score);
        }
        List<DynamicCommentLove> dynamicCommentLoveList = dynamicCommentLoveMapper.getAll(LoveStatus.LOVE);
        for (DynamicCommentLove dynamicCommentLove : dynamicCommentLoveList) {
            Integer userId = dynamicCommentLove.getUserId();
            redisCommonUtils.incrementScoreForZSet(CacheInstruction.USER_RANK, userId, score);
        }
        List<DynamicCommentReplyLove> dynamicCommentReplyLoveList = dynamicCommentReplyLoveMapper.getAll(LoveStatus.LOVE);
        for (DynamicCommentReplyLove dynamicCommentReplyLove : dynamicCommentReplyLoveList) {
            Integer userId = dynamicCommentReplyLove.getUserId();
            redisCommonUtils.incrementScoreForZSet(CacheInstruction.USER_RANK, userId, score);
        }
    }

    /**
     * 恢复用户主动评论产生的热度
     *
     * @return {@link}
     * @author 廖振辉
     */
    void recoverComment() {
        List<DynamicComment> dynamicCommentList = dynamicCommentMapper.selectAll(DeleteStatus.NO);
        Double score = topicUserHotTableConfig.getScoreByOperateType(TopicUserHotType.COMMENT);
        for (DynamicComment dynamicComment : dynamicCommentList) {
            Integer userId = dynamicComment.getUserId();
            redisCommonUtils.incrementScoreForZSet(CacheInstruction.USER_RANK, userId, score);
        }
    }

    /**
     * 恢复用户主动回复产生的热度
     *
     * @return {@link}
     * @author 廖振辉
     */
    void recoverReply() {
        List<DynamicCommentReply> dynamicCommentReplyList = dynamicCommentReplyMapper.getAll(DeleteStatus.NO);
        Double score = topicUserHotTableConfig.getScoreByOperateType(TopicUserHotType.REPLY);
        for (DynamicCommentReply dynamicCommentReply : dynamicCommentReplyList) {
            Integer userId = dynamicCommentReply.getUserId();
            redisCommonUtils.incrementScoreForZSet(CacheInstruction.USER_RANK, userId, score);
        }
    }

    /**
     * 恢复用户发布动态产生的热度
     *
     * @return {@link}
     * @author 廖振辉
     */
    void recoverDynamic() {
        List<Dynamic> dynamicList = dynamicMapper.getAll(DeleteStatus.NO);
        Double score = topicUserHotTableConfig.getScoreByOperateType(TopicUserHotType.PUBLISH_DYNAMIC);
        for (Dynamic dynamic : dynamicList) {
            Integer userId = dynamic.getUserId();
            redisCommonUtils.incrementScoreForZSet(CacheInstruction.USER_RANK, userId, score);
        }
    }

    /**
     * 恢复用户动态被点赞产生的热度
     *
     * @return {@link}
     * @author 廖振辉
     */
    void recoverDynamicBeLoved() {
        Double score = topicUserHotTableConfig.getScoreByOperateType(TopicUserHotType.DYNAMIC_BE_LOVE);
        List<UserPO> userPOList = userMapper.selectListOrderByCreateTimeDEsc(0);
        for (UserPO userPO : userPOList) {
            Integer userId = userPO.getId();
            if (userId == 0) {
                continue;
            }
            Integer dynamicBeLovedCount = dynamicLoveMapper.countLoveByUserId(userId);
            if (dynamicBeLovedCount != 0) {
                redisCommonUtils.incrementScoreForZSet(CacheInstruction.USER_RANK, userId, score);
            }
        }
    }

    /**
     * 恢复用户动态被评论产生的热度
     *
     * @return {@link}
     * @author 廖振辉
     */
    void recoverDynamicBeComment() {
//        Double score = topicUserHotTableConfig.getScoreByOperateType(TopicUserHotType.BE_COMMENT);
//        List<UserPO> userPOList = userMapper.selectListOrderByCreateTimeDEsc();
//        for (UserPO userPO : userPOList) {
//            Integer userId = userPO.getId();
//            if (userId == 0){
//                continue;
//            }
//
//        }
    }

}
